package com.vic.aliyun;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse;
import com.aliyun.teaopenapi.models.Config;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiSmartworkHrmEmployeeQuerydimissionRequest;
import com.dingtalk.api.request.OapiSmartworkHrmEmployeeQueryonjobRequest;
import com.dingtalk.api.request.OapiV2UserGetRequest;
import com.dingtalk.api.response.OapiSmartworkHrmEmployeeQuerydimissionResponse;
import com.dingtalk.api.response.OapiSmartworkHrmEmployeeQueryonjobResponse;
import com.dingtalk.api.response.OapiV2UserGetResponse;
import com.taobao.api.ApiException;


/**
 * @author victor
 */
public class Test {

    public static void main(String[] args) throws Exception {
        Config config = new Config();
        config.protocol = "https";
        config.regionId = "central";
        com.aliyun.dingtalkoauth2_1_0.Client client = new com.aliyun.dingtalkoauth2_1_0.Client(config);
        GetAccessTokenRequest getAccessTokenRequest = new GetAccessTokenRequest()
                .setAppKey("dings6r6va5xba3ln0hw")
                .setAppSecret("Z5rjP5KZGIReSg56Bd2U4heK5NsW9NtaiQbrjRzYaKBY8l4biTCbxPiakj4PI8PN");
        GetAccessTokenResponse getAccessTokenResponse = client.getAccessToken(getAccessTokenRequest);
        String accessToken = getAccessTokenResponse.getBody().accessToken;

        JSONArray jsonArray = new JSONArray();
        test1(0L, accessToken, jsonArray);
        System.out.println(jsonArray.toJSONString());

        for (Object id : jsonArray) {
            DingTalkClient cserClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
            OapiV2UserGetRequest req = new OapiV2UserGetRequest();
            req.setUserid(id.toString());
            req.setLanguage("zh_CN");
            OapiV2UserGetResponse rsp = cserClient.execute(req, accessToken);
            System.out.println(rsp.getBody());
        }
    }


    static void test1(Long offset, String accessToken, JSONArray jsonArray) throws ApiException {

        DingTalkClient querydimissionClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/queryonjob");
        OapiSmartworkHrmEmployeeQueryonjobRequest req1 = new OapiSmartworkHrmEmployeeQueryonjobRequest();
        req1.setStatusList("2,3,5,-1");
        req1.setOffset(offset);
        req1.setSize(50L);
        OapiSmartworkHrmEmployeeQueryonjobResponse rsp1 = querydimissionClient.execute(req1, accessToken);
        JSONObject jsonObject = JSONObject.parseObject(rsp1.getBody());
        Long aLong = jsonObject.getJSONObject("result").getLong("next_cursor");
        JSONArray jsonArray1 = jsonObject.getJSONObject("result").getJSONArray("data_list");
        jsonArray.addAll(jsonArray1);
        if (ObjectUtil.isNotNull(aLong)) {
            test1(aLong, accessToken, jsonArray);
        }
    }
}